<?php
class Core_Model_DbTable_Inboxs extends YouNet_Db_Table
{
    protected $_name = 'inbox';
    protected $_rowClass = 'Core_Model_Inbox';
    static private $_instance;
    
    public static function getInstance()
    {
        if (!(self::$_instance instanceof Core_Model_DbTable_Inboxs))
        {
            self::$_instance = new Core_Model_DbTable_Inboxs();
        }
        return self::$_instance;
    }
    
    public function insertMessage($params)
    {
        //$receiver_ids = explode(',', $params['receiver_ids']);
        //TODO: fix ignore receiver
        if (empty($params['id_social'])) {
            $params['id_social'] = NULL;
        }
        $data = array(
            'id_request'    =>  $params['id_request'],
            'id_sender'     =>  $params['id_social'],            
            'id_page'       =>  $params['id_page'],
            'id_gift'       =>  $params['id_gift'],
            'message'       =>  $params['message_content']
            );        
        $this->insert($data);
//        foreach ($receiver_ids as $receiver_id) {            
//            $data = array_merge($data, array('id_to'=>$receiver_id));
//            $this->insert($data);
//        }        
    }
    public function getMessage($params)
    {
        $select = $this->select()
            ->setIntegrityCheck(false)
            ->from('inbox')
            ->join('images','images.id = inbox.id_gift',array('name','file_name'))
            ->where('id_request = ?', $params['rid'])            
            ->order('id DESC')
            ->limit(1);        
        $rowset = $this->fetchAll($select);
        $row = NULL;
        if (!empty ($rowset))
            $row = $rowset->current();
        //
        return $row;
    }
}
?>
